# -*- coding: utf-8 -*-

import os
import sys
import datetime
import pytz
import jinja2

from openerp import SUPERUSER_ID, models
from openerp.osv import fields
from openerp.osv import orm
from openerp.osv import osv

if hasattr(sys, 'frozen'):
    # When running on compiled windows binary, we don't have access to package loader.
    path = os.path.realpath(os.path.join(os.path.dirname(__file__), '..', 'views'))
    loader = jinja2.FileSystemLoader(path)
else:
    loader = jinja2.PackageLoader('openerp.addons.hfsy_lodop', "report")

env = jinja2.Environment(loader=loader, autoescape=True)

#jinja2模版自定义过滤器
def strftime(str, fmt):
    dt = datetime.datetime.strptime(str, '%Y-%m-%d %H:%M:%S')
    dt = dt.replace(tzinfo=pytz.utc).astimezone(pytz.timezone('Asia/Shanghai'))
    return dt.strftime(fmt.encode('utf-8')).decode('utf-8')

env.filters['strftime'] = strftime


# 采购订单打印扩展，调用lodop打印
class purchase_order_lodop(osv.osv):
    _inherit = 'purchase.order'
    #打印采购订单函数
    def print_quotation_lodop(self, cr, uid, ids, context=None):
        '''
        This function prints the sales order and mark it as sent, so that we can see more easily the next step of the workflow
        '''
        assert len(ids) == 1, 'This option should only be used for a single id at a time'
        return self.pool['report'].get_action(cr, uid, ids, 'report_purchase_order_ext', context=context)

    #打印采购入库单函数
    def print_purchase_order_stock_move_in_lodop(self, cr, uid, ids, context=None):
        '''
        This function prints the sales order and mark it as sent, so that we can see more easily the next step of the workflow
        '''
        assert len(ids) == 1, 'This option should only be used for a single id at a time'
        return self.pool['report'].get_action(cr, uid, ids, 'report_purchase_order_stock_move_in', context=context)

# render采购订单打印模板
class report_purchase_order_lodop(osv.AbstractModel):
    """Model used to embed lodop reports"""
    _name = 'report.report_purchase_order_ext'
    _template = None
    _wrapped_report_class = None

    def render_html(self, cr, uid, ids, data=None, context=None):
        model = 'purchase.order'
        report_obj = self.pool[model]
        purchase_order = report_obj.browse(cr, uid, ids, context=context)

        curUser = self.pool.get('res.users').browse(cr,uid,uid,context=None)

        template = env.get_template("purchase_order_print.html")
        return template.render({
            'purchase_order': purchase_order,
            'curUser': curUser,
            'debug': context,
            })


# render采购入库单打印模板
class report_purchase_order_stock_move_in_lodop(osv.AbstractModel):
    """Model used to embed lodop reports"""
    _name = 'report.report_purchase_order_stock_move_in'
    _template = None
    _wrapped_report_class = None

    def render_html(self, cr, uid, ids, data=None, context=None):
        model = 'purchase.order'
        report_obj = self.pool[model]
        purchase_order = report_obj.browse(cr, uid, ids, context=context)

        curUser = self.pool.get('res.users').browse(cr,uid,uid,context=None)

        template = env.get_template("purchase_order_stock_move_in_print.html")
        return template.render({
            'purchase_order': purchase_order,
            'debug': context,
            })
